package wang.hex;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;

/**
 * @Author fox
 * @Class TracerEnhancer
 * @Description
 * @Date 2020/12/24 11:40
 */
public class TracerEnhancer extends Enhancer {
    private Callback callback;

    /**
     * 重写callback方法 省去反射去获取父类的逻辑
     *
     * @param callback
     */
    public void setCallback(Callback callback) {
        this.callback = callback;
        super.setCallback(callback);
    }

    /**
     * 骗过构造方法检测
     * @return
     */
    public Object create() {
        return super.create(new Class[]{int.class}, new Object[]{0});
    }

    /**
     * 重写建立动态代理实体的方法
     *
     * @param type
     * @return
     * @throws Exception
     */
    protected Object firstInstance(Class type) throws Exception {
        //注册拦截信息
        registerCallbacks(type, new Callback[]{callback});
        return CreateObjectSkipConstruction.getObject(type);
    }


}
